<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable" @row-click="handleTestDataChildRowClick">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-popconfirm
          title="是否确认清除所有未生效数据？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleNValidDelete()"
        >
          <a-button type="error" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:delete-outlined" /> {{ t('清除未生效数据') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认删除？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleBatchDelete()"
        >
          <a-button type="error" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:delete-outlined" /> {{ t('批量删除') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认协同路线检查？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleCheck()"
        >
          <a-button type="success" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:video-camera-twotone" /> {{ t('协同路线检查') }}
          </a-button>
        </a-popconfirm>

        <a-popconfirm
          title="是否确认价格生效？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleStatus()"
        >
          <a-button type="primary" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:check-outlined" /> {{ t('价格生效') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认价格全部生效？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleAllStatus()"
        >
          <a-button type="warning" v-auth="'setting:price:edit'">
            <Icon icon="ant-design:check-outlined" /> {{ t('全部生效') }}
          </a-button>
        </a-popconfirm>

        <!-- <a-button type="primary" @click="handleExport()"  v-auth="'setting:price:edit'">
                <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
              </a-button> -->

        <a-button type="primary" @click="handleImport()" v-auth="'setting:price:edit'">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="btnExport()" v-auth="'setting:price:view'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopExtendShopExtendClsIndex',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { PageWrapper } from '/@/components/Page';
  import ListView from './list.vue';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    valid,
    treeData,
    updateCheck,
    listData,
    batchDeletePrice,
    clearNvalid,
  } from '/@/api/setting/price/wecharCusInvPriceRoute';
  import { useModal } from '/@/components/Modal';
  import FormImport from '../wecharCusInvPriceRoute/formImport.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { handleExport } from '/@/utils/export';
  const { t } = useI18n('setting.price.wecharCusInvPriceRoute');

  const treeCode = ref<string>('');
  const invCode = ref<string>('');

  const viewList = ref();

  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('终端客户'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('终端客户'),
        field: 'endCusCode',
        // fieldLabel: 'endBasCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: treeData,
          allowClear: true,
        },
      },
      {
        label: t('产品编码'),
        field: 'invCode',
        component: 'Input',
        componentProps: {
          allowClear: true,
        },
      },
      {
        label: t('生效状态'),
        field: 'validStatus',
        component: 'Select',
        defaultValue: '0',
        componentProps: {
          dictType: 'sys_valid',
          allowClear: true,
        },
      },

      {
        label: t('检验异常'),
        field: 'brouteErr',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('生效状态'),
      dataIndex: 'validStatus',
      key: 'a.valid_status',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_valid',
    },
    {
      title: t('检验异常'),
      dataIndex: 'brouteErr',
      key: 'a.broute_err',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('终端客户'),
      dataIndex: 'endCusCode',
      key: 'a.end_cus_code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('终端客户名称'),
      dataIndex: 'endBasCus.name',
      key: 'cus2.name',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('产品编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('产品名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('协同路线'),
      dataIndex: 'croute',
      key: 'a.croute',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('协同单价'),
      dataIndex: 'routePrice',
      key: 'a.route_price',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('协同描述'),
      dataIndex: 'routeDesc',
      key: 'a.route_desc',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('导入日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('生效日期'),
      dataIndex: 'dstarDate',
      key: 'a.dstar_date',
      sorter: true,
      width: 120,
      format: 'YYYY-MM-DD',
      align: 'left',
    },
    {
      title: t('异常提示'),
      dataIndex: 'routeErr',
      key: 'a.route_err',
      sorter: true,
      width: 260,
      align: 'left',
    },
  ];

  const [registerTable, { reload, getSelectRows, getForm }] = useTable({
    api: listData,
    beforeFetch: (params) => {
      params.ope = '2';
      return params;
    },
    columns: tableColumns, //配置表格内容数组对象
    // actionColumn: actionColumn, //配置表格右边操作按钮
    formConfig: searchForm, //配置表单内容
    showTableSetting: true, //表格右上角3个默认按钮
    useSearchForm: true, //表单是否展示
    canResize: true, //表格是否flex布局
    rowSelection: {
      type: 'checkbox',
    },
  });
  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }
  async function btnExport() {
    var formdata = getForm().getFieldsValue();
    formdata = { ...formdata };
    debugger;
    handleExport('/setting/price/wecharCusInvPriceRoute/exportData', formdata);
  }

  async function handleTestDataChildRowClick(record: Recordable) {
    treeCode.value = record.endCusCode;
    invCode.value = record.invCode;

    record.onEdit?.(true, false);
    // reload2({
    //   searchInfo: { 'route.id': record.id },
    // });
  }
  function handleSuccess() {
    reload();
  }
  async function handleStatus() {
    if (getSelectRows().length) {
      try {
        let selIds = '';
        getSelectRows().forEach((item) => {
          selIds += item.id + ',';
        });
        const res = await valid({ selIds });
        showMessage(res.message);
      } catch (error: any) {
        console.log('error', error);
      } finally {
        handleSuccess();
        viewList.value.handleSuccess1();
      }
    } else {
      showMessage('请先选择数据！！');
    }
  }
  //全部未生效数据->>生效
  async function handleAllStatus() {
    try {
      const res = await valid({ ope: 'ALL' });
      showMessage(res.message);
    } catch (error: any) {
      console.log('error', error);
    } finally {
      handleSuccess();
      viewList.value.handleSuccess1();
    }
  }

  async function handleCheck() {
    try {
      let selIds = '';
      getSelectRows().forEach((item) => {
        selIds += item.id + ',';
      });
      const res = await updateCheck({ selIds });
      showMessage(res.message);
    } catch (error: any) {
      console.log('error', error);
    } finally {
      handleSuccess();
    }
  }

  // 删除
  async function handleBatchDelete() {
    if (getSelectRows().length) {
      try {
        let selIds = '';
        getSelectRows().forEach((item) => {
          selIds += item.id + ',';
        });
        const res = await batchDeletePrice({ selIds });
        showMessage(res.message);
      } catch (error: any) {
        console.log('error', error);
      } finally {
        handleSuccess();
      }
    } else {
      showMessage('请先选择数据！！');
    }
  }
  // 清除未生效
  async function handleNValidDelete() {
    try {
      const res = await clearNvalid({});
      showMessage(res.message);
    } catch (error: any) {
      console.log('error', error);
    } finally {
      handleSuccess();
    }
  }
</script>
